
var path = require('path');
var log4js = require(path.resolve(__dirname, '../config/logger'));
var logger = log4js.getLogger('status');
const Paragraph = require('./paragraph');
class Article {
  constructor(options) {
    this.id = options.id;
    this.name = options.name || '';
    this.channel = options.channel || 1;
    this.template = options.template;
    this.contents = options.contents;
    this.paragraphsMap = {};
    this.title = options.title || options.name;
    this.status = options.status;
    this.titleContent = '';
    this.content = '';
    this.autoPublish = false;
    this.autoParams = options.autoParams || {};
  }

  compile(options) {
    var that = this;
    return new Promise(function (resolve, reject) {
      var force = options.force;
      var titleParagraph = null;
      var contentParagraphs = [];
      var promiseArrs = [];
      var serialRequest = [];
      if (that.title && that.title.length > 0) {
        titleParagraph = new Paragraph(that.title[0].id);
        titleParagraph.setAutoParams(that.autoParams);
        titleParagraph.setForce(force);
        if (!that.paragraphsMap[that.title[0].id]) {
          that.paragraphsMap[that.title[0].id] = titleParagraph;
          serialRequest.push(titleParagraph);
          //promiseArrs.push(titleParagraph.getContent());
        }
      }
      that.contents.forEach(element => {
        if (!that.paragraphsMap[element.id]) {
          var paragraph = new Paragraph(element.id);
          paragraph.setAutoParams(that.autoParams);
          paragraph.setForce(force);
          that.paragraphsMap[element.id] = paragraph;
          serialRequest.push(paragraph);
          //promiseArrs.push(paragraph.getContent())
        }
      });
      var length = serialRequest.length;
      var index = 0;
      // 串行解析文章段落
      var paragraphGo = function() {
        if (index == length) {
          that.getRealContent();
          resolve('ok');
        } else {
          serialRequest[index].getContent().then(function(result) {
            index++;
            paragraphGo(index);
          }).catch(function(error) {
            console.log(error);
            reject(error);
          })
        }
      }
      paragraphGo();
    })


  }
  // publish() {
  //   if (this.content) {

  //   }
  // }
  // 传入变动参数
  setAutoParams(autoParams) {
    this.autoParams = autoParams || {};
  }
  getRealContent() {
    // var titleContent = '';
    if (this.title && this.title.length > 0) {
      this.titleContent = this.paragraphsMap[this.title[0].id].content;
    }
    this.content = '';
    this.titleContent = this.titleContent.replace(/<[^>]+>/g,"")
    this.contents.forEach(element => {
      if (this.paragraphsMap[element.id]) {
        this.content += this.paragraphsMap[element.id].content;
      }
    });
		let countArr = this.content.match(/<script>/g) || [];
		if(countArr.length != 0){
			let arr = this.content.split('<div id=');
			arr.forEach((item, index) =>{
				if(index != 0){
					arr[index] = item.replace(/dtChart/g, 'dtChart' + index);
				}
			});
//			logger.info('arr: ' + arr[1], "所有段落");
			this.content = arr.join('<div id=');
    }
    //this.addStyleSheets();
  }
  addStyleSheets() {
    const cssStyles = `.xg_table_wrapper{ width: 100%; overflow-x: auto;}
      .xg_table_wrapper::-webkit-scrollbar { height:6px;}
      .xg_table_wrapper::-webkit-scrollbar-track { background-color: #f0f0f0;  border-radius: 3px;}
      .xg_table_wrapper::-webkit-scrollbar-thumb { border-radius: 3px; background:rgba(0,0,0,0.3); }
      .xg_table_wrapper::-webkit-scrollbar-thumb:window-inactive { background:rgba(0,0,0,0.1);}
      .xg_table_wrapper table{ font-size: 14px; width: 100%;}
      .xg_table_wrapper th{ border: 1px #e4e4e4 solid; border-collapse: collapse; padding: 8px; word-break: break-all; background-color: #f2f1ef; vertical-align: middle; font-weight: bold; }
      .xg_table_wrapper td{ border: 1px #e4e4e4 solid; border-collapse: collapse; padding: 8px; word-break: break-all;}
      .xg_table_wrapper th,
      .xg_table_wrapper td{ min-width: 75px; vertical-align: middle; }`;
    const styleDoms = `<style type="text/css">${cssStyles}</style> `;
    this.content = styleDoms + this.content;
  }

}

module.exports =  Article;